import createMessage from './components/createMessage'
import { ColumnProps, ImageProps, UserProps } from './store'

export function generateFitUrl(data: ImageProps, width: number, height: number, format = ['m_pad']) {
  if (data && data.url) { //有图片
    const formatStr = format.reduce((prev, current) => {  //数组的reduce方法
      return current + ',' + prev   //返回结果是m_pad
    }, '')
    //保存在store中ImageProps接口类型的属性中
    //?.....代表使用了阿里云oss的图片处理，这里的功能是把图片缩放到宽为w_${width}、高为h_${height},m_pad为参数
    data.fitUrl = data.url + `?x-oss-process=image/resize,${formatStr}h_${height},w_${width}` 
  }
}

export function addColumnAvatar(data: ColumnProps | UserProps, width: number, height: number) {
  if (data.avatar) {  //如果专栏或者用户有头像
    generateFitUrl(data.avatar, width, height)
  } else {  //专栏没有图片的话，使用默认图片
    const parseCol = data as ColumnProps
    data.avatar = {
      fitUrl: require(parseCol.title ? '@/assets/column.jpg' : '@/assets/avatar.jpg') //引入本地文件
    }
  }
}
export interface ValidateResult {
  passed: boolean;
  error: string;
}

interface CheckCondition{
    format?:string[],
    size?:number
}
export interface CheckFileFormat {
  fileFormat?: string[];
  fileSize?: number
}

type ErrorType = 'size' | 'format' | null //这里定义了出错的类型，分别是文件大小出错，文件格式出错，无出错
export function beforeUploadCheck(file:File,condition:CheckCondition){
    const {format,size} =condition
    const isValidFormat = format ? format.includes(file.type):true  //format为空代表没有格式要求，直接返回true
    const isValidSize = size?(file.size/1024/1024):true //size为空代表没有上传文件大小要求，直接返回true
    let error:ErrorType = null
    if(!isValidFormat){
        error='format'
    }
    if(!isValidSize){
        error='size'
    }
    return {
        passed:isValidFormat && isValidSize,
        error
    }
}

export function checkUploadFileFormat (file: File, format: CheckFileFormat): ValidateResult {
  const validateResult: ValidateResult = {
    passed: true,
    error: ''
  }
  const { fileFormat, fileSize } = format
  if (fileFormat) {
    if (!fileFormat.includes(file.type)) {
      validateResult.passed = false
      validateResult.error = `只能上传${fileFormat.join(',')}文件`
      return validateResult
    }
  }
  if (fileSize) {
    if ((file.size / 1024 / 1024) > fileSize) {
      validateResult.passed = false
      validateResult.error = `文件大小不能超过${fileSize}M`
      return validateResult
    }
  }
  return validateResult
}

interface TestProps{
  _id:string;
  name:string;
}
const testData:TestProps[]=[{_id:'1',name:'a'},{_id:'2',name:'b'},{_id:'3',name:'c'}]  //数组

export const arrToObj = <T extends {_id?:string}>(arr:Array<T>)=>{  //将数组转换为对象，T的类型是TestProps
  return arr.reduce((prev,current)=>{ //prev为初始值以及上一次调用产生的值，current为当前正在处理的值
    if(current._id){
      prev[current._id]=current //往空对象中添加属性
    }
    return prev
  },{} as {[key:string]:T}) //初始值为空对象，断言为{[key:string]:T}类型
}

export const commonUploadCheck = (file: File) => {
  const result = beforeUploadCheck(file, { format: ['image/jpeg', 'image/png'], size: 1 })
  const { passed, error } = result
  if (error === 'format') {
    createMessage('上传图片只能是 JPG/PNG 格式!', 'error')
  }
  if (error === 'size') {
    createMessage('上传图片大小不能超过 1Mb', 'error')
  }
  return passed
}

const result = arrToObj(testData)
export const objToArr = <T>(obj:{[key:string]:T})=>{  //将对象变为数组，T的类型是TestProps
  return Object.keys(obj).map(key=>obj[key])
}
const testData2:{[key:string]:TestProps}={  //指定testData2的泛型的格式，key指的是1,2,TestProps是对象
  1:{_id:'1',name:'a'},
  2:{_id:'2',name:'b'}
}

const result2 = objToArr(testData2)